package com.RabbitMQ.routing.Producter;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by thinkjoy on 2017/6/29.
 * 采用路由的方式对不同的消息进行过滤
 */
public class RoutingSendDirect {
    private static final String EXCHANGE_NAME = "direct_logs";
//    路由关键字
    private static final String[] routingKeys=new String[]{"info" ,"warning", "error"};

    public static void main(String[] args)throws IOException,TimeoutException{
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection=factory.newConnection();
        Channel channel=connection.createChannel();
//       声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME,"direct");
//        发送消息
        for (String rountingkey:routingKeys) {
            String message= "RoutingSendDirect Send the message level:" + rountingkey;
            channel.basicPublish(EXCHANGE_NAME,rountingkey,null,message.getBytes());
            System.out.println("RoutingSendDirect Send"+rountingkey +"':'" + message);
        }
        channel.close();
        connection.close();
    }
}
